/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.logs.service.impl;

import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.logs.mapper.LogsCaseDocumentSerialRecordMapper;
import com.icinfo.cloud.provider.logs.model.LogsCaseDocumentSerialRecord;
import com.icinfo.cloud.provider.logs.service.ILogsCaseDocumentSerialRecordService;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 描述:  logs_case_document_serial_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2023年01月07日
 */
@Service
public class LogsCaseDocumentSerialRecordServiceImpl extends MyBatisServiceSupport implements ILogsCaseDocumentSerialRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(LogsCaseDocumentSerialRecordServiceImpl.class);

    @Autowired
    private LogsCaseDocumentSerialRecordMapper logsCaseDocumentSerialRecordMapper;

    /**
     * 【V1.10.2】描述：保存序列号记录
     * @author tianrunjia
     * @date 2023-01-07
     * @param logsCaseDocumentSerialRecord
     */
    @Override
    public void saveLogsCaseDocumentSerialRecord(LogsCaseDocumentSerialRecord logsCaseDocumentSerialRecord) {
        logsCaseDocumentSerialRecordMapper.insert(logsCaseDocumentSerialRecord);
    }

    /**
     * 【V1.12.0】描述：更新序列号异常状态
     * @author tianrunjia
     * @date 2023-08-23
     * @param logsCaseDocumentSerialRecord
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void modLogsCaseDocumentSerialRecordException(LogsCaseDocumentSerialRecord logsCaseDocumentSerialRecord) {
        if(StringUtils.isNotBlank(logsCaseDocumentSerialRecord.getDocumentCatalogCode()) &&
                StringUtils.isNotBlank(logsCaseDocumentSerialRecord.getBusiId()) ){
            logsCaseDocumentSerialRecordMapper.updateLogsCaseDocumentSerialRecordException(logsCaseDocumentSerialRecord);
        }else {
            throw new BusinessException("更新序列号异常状态异常！");
        }
    }

    /**
     * 【V1.12.0】描述：将异常的状态更新为正常状态
     * @author tianrunjia
     * @date 2023-08-23
     * @param logsCaseDocumentSerialRecord
     */
    @Override
    public void modLogsCaseDocumentSerialExcepToNormal(LogsCaseDocumentSerialRecord logsCaseDocumentSerialRecord) {
        if(StringUtils.isNotBlank(logsCaseDocumentSerialRecord.getDocumentCatalogCode()) &&
                StringUtils.isNotBlank(logsCaseDocumentSerialRecord.getOrgCode()) &&
                StringUtils.isNotBlank(logsCaseDocumentSerialRecord.getSourceType()) &&
                StringUtils.isNotBlank(logsCaseDocumentSerialRecord.getLineCode()) &&
                StringUtils.isNotBlank(logsCaseDocumentSerialRecord.getYear()) ){
            logsCaseDocumentSerialRecordMapper.updateLogsCaseDocumentSerialExcepToNormal(logsCaseDocumentSerialRecord);
        }else {
            throw new BusinessException("更新序列号正常状态异常！");
        }
    }

    /**
     * 【V1.12.0】描述：从异常状态的序号池中获取最小的序号
     * @author tianrunjia
     * @date 2023-08-23
     * @param logsCaseDocumentSerialRecord
     */
    @Override
    public Long getLogsCaseDocumentSerialRecordException(LogsCaseDocumentSerialRecord logsCaseDocumentSerialRecord) {
        LogsCaseDocumentSerialRecord lastRecord=logsCaseDocumentSerialRecordMapper.selectLogsCaseDocumentSerialRecordException(logsCaseDocumentSerialRecord);
        if(null==lastRecord){
            return null;
        }
        return lastRecord.getSerial();
    }


}